/**
 * @Author yinyunqi
 * @date 2017年12月25日
 * @Content 
 */
package com.yongqi.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.yongqi.model.Menu;
import com.yongqi.service.MenuService;

@RequestMapping("/menu")
@Controller
public class MenuController {
	@Autowired
	MenuService menuService;

	@RequestMapping("/menulist")
	public ModelAndView getMenuList() {
		List<Menu> parentMenu = menuService.getParentMenu();
		List<Menu> childMenu = menuService.getChildMenu();
		List<Menu> menuList = new ArrayList<Menu>();
		for (Menu tempMenu : parentMenu) {
			if (tempMenu.getMenu_id() == 4) {
				tempMenu.setChildMenus(childMenu);
			}
			menuList.add(tempMenu);
		}
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("index");
		modelAndView.addObject("menuLists", menuList);
		String familyAccount = (String) SecurityUtils.getSubject().getSession().getAttribute("currentId");
		System.out.println("familyAccount"+familyAccount);
		return modelAndView;
	}

	@ResponseBody
	@RequestMapping("/menujson")
	public String menujson() {
		List<Menu> parentMenu = menuService.getParentMenu();
		List<Menu> childMenu = menuService.getChildMenu();
		Map<Menu, List<Menu>> menuList = new HashMap<Menu, List<Menu>>();
		List<Menu> tempChildMenu = null;// 创建临时子菜单
		for (Menu parent_Menu : parentMenu) {
			tempChildMenu = new ArrayList<Menu>();
			for (Menu child_Menu : childMenu) {
				if (parent_Menu.getMenu_id() == child_Menu.getParent_id()) {
					tempChildMenu.add(child_Menu);
				}
			}
			menuList.put(parent_Menu, tempChildMenu);
		}
		JSONObject object = new JSONObject();
		object.put("result", menuList);
		return object.toJSONString();
	}
}
